/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MyCalculator;

import java.util.ArrayList;
import java.util.Stack;

/**
 *
 * This class will create all the utilities needed for the calculator's GUI
 * including the Stack etc. *
 */
public class Calculator_GUI_Utilities {
    Stack <Double>Calc_Stck;
    BackEndCalculator calc;
    Calculator_GUI_Utilities(){
        Calc_Stck=new Stack<Double>();
        calc=new BackEndCalculator();
    }

    public void Push_Number(double inputNum){
        this.Calc_Stck.push(inputNum);
    }

    public double doCalculation(int CalType){
        double result=0;
        if(CalType==18){
            //indicates the calculator is doing swaping
            double firstNum=Calc_Stck.pop();
            double secondNum=Calc_Stck.pop();

            Calc_Stck.push(firstNum);
            Calc_Stck.push(secondNum);

            return 1;
        }

        if(CalType>0&&CalType<=13){
            //in this range, the calculation only requires 1 input
            double inputNum=(double)(Calc_Stck.pop());
            result=calc.singleInputCalculation(inputNum, CalType);
            Calc_Stck.push(result);
        }else if(CalType<=19){
            //in this rangem the calculation requires 2 inputs
            double inputNum2=(double)(Calc_Stck.pop());
            double inputNum1=(double)(Calc_Stck.pop());
            //inputNum1 goes into the first slot, inputNum2 goes into the second slot
            //say, inputNum1=5 inputNum1=2, and the claculation is - --> 5-2
            result=calc.TwoInputsCalculation(inputNum1, inputNum2, CalType);
            System.out.println("In Calc: "+result);
            Calc_Stck.push(result);
        }else{
            double[] inputArr = new double [Calc_Stck.size()];
            int i=0;
            while(!Calc_Stck.isEmpty()){
                inputArr[i]=Calc_Stck.pop();
                i++;
            }
            result=calc.MultipleInputsCalculation(inputArr, CalType);
            Calc_Stck.push(result);
        }
        return result;
    }


    //PolyString would looks like x^3+2x^2-x+1
    public double[][] parsePoly(String PolyString)throws PolyStringFormatException{
    //public double[][] parsePoly(String PolyString)throws PolyStringFormatException{
        ArrayList <Double>tempAL=new ArrayList<Double>();
        double polyPara[][];
        int polySize;
        ArrayList <String>TokenizedStr=new ArrayList<String>();
        //First step is to seperate the String by + and - sign
        int PreDelimPosition=0;
        for (int i=0;i<PolyString.length();i++){
            if(PolyString.charAt(i)=='+'||PolyString.charAt(i)=='-'){
                //hits a delim
                if(i!=0){
                    String seg=PolyString.substring(PreDelimPosition, i);
                    TokenizedStr.add(seg);
                }
                PreDelimPosition=i;
            }
        }
        String seg=PolyString.substring(PreDelimPosition);
        TokenizedStr.add(seg);
        int termNum=TokenizedStr.size();
        //This is the veriable that contains the information for the poly
        polyPara=new double [termNum][2];
        polySize=termNum;
        for (int j=0;j<TokenizedStr.size();j++){
            int sign;
            double coeInt;
            int powInt=1;
            String coeStr=null;
            String powStr=null;
            String []splitted=TokenizedStr.get(j).split("x");
            if(splitted.length>0){
                coeStr=TokenizedStr.get(j).split("x")[0];
            }
            if(splitted.length>1){
                powStr=TokenizedStr.get(j).split("x")[1];
            }
            
            //Need to procees the Coe
            if(coeStr==null){
                sign=1;
                coeInt=1;
            }else if(coeStr.equals("")){
                sign=1;
                coeInt=1;
            }else if(coeStr.equals("+")){
                sign=1;
                coeInt=1;
            }else if(coeStr.equals("-")){
                sign=-1;
                coeInt=1;
            }else if(coeStr.charAt(0)=='+'){
                sign=1;
                coeInt=Double.valueOf(coeStr.substring(1));
            }else if(coeStr.charAt(0)=='-'){
                sign=-1;
                coeInt=Double.valueOf(coeStr.substring(1));
            }else{
                sign=1;
                coeInt=Double.valueOf(coeStr);
            }
            //System.out.println("Coe= "+coeInt);
            //System.out.println("sign= "+sign);

            if(powStr==null){
                if(TokenizedStr.get(j).contains("x"))
                    powInt=1;
                else
                    powInt=0;
            }else if (powStr.equals("")){
                powInt=1;
            }else if(powStr.charAt(0)=='^'){
                powInt=Integer.valueOf(powStr.substring(1));
            }else{
                throw new PolyStringFormatException();
            }
            //System.out.println("powInt= "+powInt);
            //System.out.println(TokenizedStr.get(j));
            polyPara[j][0]=coeInt*sign;
            polyPara[j][1]=powInt;
        }
        
        for (int i=0;i<polySize; i++){
            System.out.print("("+polyPara[i][0]+ "," +(int)polyPara[i][1] + ") ");
            
        }
        System.out.println();
        return polyPara;
    }



}